package com.hdaccp.foodsales.controller;


import com.hdaccp.foodsales.entity.Gonghuo;
import com.hdaccp.foodsales.service.GonghuoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/gong")
public class GonghuoController {

    @Resource
    GonghuoService gonghuoService;



    @RequestMapping("/goUser.action")
    public String goUserPage(){
        return "user";
    }

    /**
     * 分页查询所以user表
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/userList.action")
    public Map<String,Object> getUserList(
            @RequestParam(value="pageNumber",defaultValue="0",required=false)int pageNumber,
            @RequestParam("pageSize")int pageSize){
        Map<String,Object>map=gonghuoService.getUserList(pageNumber, pageSize);

        return map;
    }


    /**
     * 验证用户登录账号是否唯一
     * @param account
     * @param uid
     * @return
     */
    @ResponseBody
    @RequestMapping("/validAccount.action")
    public Map<String,Object> validUserAccount(
            @RequestParam("account")String account,
            @RequestParam(value="uid",defaultValue="0",required=false)int uid){
        Map<String,Object> map=new HashMap<String, Object>();
        boolean flag=gonghuoService.validUserAccount(account, uid);
        if(flag){
            map.put("msg", "此供货商名可用");
        }else{
            map.put("msg", "此供货商名不可用");
        }
        return map;
    }


    /**
     * 添加用户
     * @param gonghuo
     * @return
     */
    @ResponseBody
    @RequestMapping("/saveUser.action")
    public int saveUser(Gonghuo gonghuo, HttpServletRequest request){
        int i=gonghuoService.saveUser(gonghuo, request);
        return i;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delUser.action")
    public int delUser(@RequestParam("uid")int id){
        int i=gonghuoService.delUser(id);
        return i;
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/delUsers.action")
    public int delUsers(@RequestParam("ids")int[] ids){
        int i=gonghuoService.delUsers(ids);
        return i;
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUserById.action")
    public Map<String,Object> queryUserById(@RequestParam("uid")int id){
        Map<String,Object>map=new HashMap<String, Object>();

        Gonghuo user=gonghuoService.queryUserById(id);
        map.put("user", user);

        return map;
    }



    /**
     * 导出所有
     * @param response
     */
    @RequestMapping("/export.action")
    public void ExportExcel(HttpServletResponse response){
        gonghuoService.ExportUserList(response);
    }

    /**
     * 条件查询
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUserByCon.action")
    public Map<String,Object> getUseByCon(
            @RequestParam("pageNumber")Integer pageNumber,
            @RequestParam("pageSize")Integer pageSize,
            @RequestParam(value="ghid",required=false,defaultValue="0")Integer ghid,
            @RequestParam(value="ghxm",required=false,defaultValue="values")String ghxm,
            @RequestParam(value="deptId",required=false,defaultValue="0")Integer deptId){
        return gonghuoService.getUserListByCon((pageNumber-1)*pageSize, pageNumber*pageSize, ghid, ghxm,deptId);
    }


}
